package com.googlecode.yami;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public final class TextTransfer implements ClipboardOwner {

    StringSelection str;
    String result = "";
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    
    public void setClipboardContents(String string) {
	str = new StringSelection(string);
	clipboard.setContents(str, this);
    }

    public String getClipboardContents() {

	Transferable contents = clipboard.getContents(null);
	boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
	if (hasTransferableText) {
	    try {
		result = (String) contents.getTransferData(DataFlavor.stringFlavor);
	    } catch (UnsupportedFlavorException e) {
		System.out.println("Ooops");
		e.printStackTrace();
	    } catch (IOException e) {
		System.out.println("Godzilla ate the clipboardz");
		e.printStackTrace();
	    }
	}
	
	return result;
    }

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
	// TODO Auto-generated method stub

    }

}
